package com.summer.media.service.impl;

import com.summer.media.mapper.MediaFilesMapper;
import com.summer.media.mapper.MediaProcessHistoryMapper;
import com.summer.media.mapper.MediaProcessMapper;
import com.summer.media.pojo.MediaFiles;
import com.summer.media.pojo.MediaProcess;
import com.summer.media.pojo.MediaProcessHistory;
import com.summer.media.service.MediaFileService;
import com.summer.media.service.MediaProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class MediaProcessServiceImpl implements MediaProcessService {

    @Autowired
    private MediaProcessMapper mediaProcessMapper;

    @Autowired
    private MediaFilesMapper mediaFilesMapper;

    @Autowired
    private MediaProcessHistoryMapper mediaProcessHistoryMapper;

    @Override
    public List<MediaProcess> getTask(int total, int index, int taskNum) {
        return mediaProcessMapper.getTask(total, index, total);
    }

    @Override
    public boolean getLock(Long id) {
        return mediaProcessMapper.getLock(id) > 0;
    }

    @Transactional
    @Override
    public boolean saveResult(MediaProcess mediaProcess, String filePath, String url) {
        // 1. 更新process表status、media_file表url： update media_process set status = 2 where id = #{id}; update media_file set url = #{newObjectName} where file_id = #{fileId}
        // 1.1 更新file表
        String fileId = mediaProcess.getFileId();
        MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
        if (mediaFiles == null){
            log.info("结果处理失败：查询media_file表无结果");
            return false;
        }
        mediaFiles.setFilePath(filePath);
        mediaFiles.setUrl(url);
        int updateFileTable = mediaFilesMapper.updateById(mediaFiles);
        if (updateFileTable <= 0){
            log.info("结果处理失败：更新media_file表失败");
            return false;
        }
        // 1.2 更新process表
        mediaProcess.setUrl(url);
        mediaProcess.setStatus("2");
        mediaProcess.setFinishDate(LocalDateTime.now());
        int updateProcessTable = mediaProcessMapper.updateById(mediaProcess);
        if (updateProcessTable<=0){
            log.info("结果处理失败：更新media_process表失败");
            return false;
        }

        // 2. 添加到历史表: insert into media_process_history values(xxx,xxx,xxx)
        MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
        BeanUtils.copyProperties(mediaProcess, mediaProcessHistory);
        int insert = mediaProcessHistoryMapper.insert(mediaProcessHistory);
        if (insert<=0){
            log.info("结果处理失败：新增history表失败");
            return false;
        }
        // 3. 删除process表记录  delete from media_process where id = id
        int deleteById = mediaProcessMapper.deleteById(mediaProcess.getId());
        if (deleteById<=0){
            log.info("结果处理失败：删除process表失败");
            return false;
        }
        return true;
    }


}
